package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.Result;
import com.dongdong.em.ResultEmnu;
import com.dongdong.pojo.*;
import com.dongdong.service.OrderService;
import com.dongdong.vo.OrderVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderController {
    @Reference(timeout = 500000)
    private OrderService orderService;

    @RequestMapping("toHome")
    public String orderPay(){
        return "home-index";
    }
    @RequestMapping("orderRecevie")
    public String orderRecevie(){
        return "home-order-receive";
    }
    @RequestMapping("orderPay")
    public String homeOrderPay(){
        return "home-order-pay";
    }
    @RequestMapping("orderSend")
    public String orderSend(){
        return "home-order-send";
    }
    @RequestMapping("orderConllect")
    public String orderConllect(){
        return "home-person-collect";
    }
    @RequestMapping("footmark")
    public String footmark(){
        return "home-person-footmark";
    }
    @RequestMapping("orderEvalute")
    public String orderEvalute(){
        return "home-order-evaluate";
    }
    @RequestMapping("toHomeAddress")
    public String toHomeAddress(Model model){
         List<TbProvinces> tbProvincesList = orderService.selectProvincesList();
         model.addAttribute("provincesList",tbProvincesList);
         return "home-setting-address";
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public Result getInfo(){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        try{
            List<TbAddress> addressList = orderService.adressGetInfo(user.getUsername());
            return Result.respone(ResultEmnu.SUCCESS).setData(addressList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("deleteById/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable("id")Long id){
        int i = orderService.deleteById(id);
        if ( i > 0){
            return Result.respone(ResultEmnu.SUCCESS).setData(i);
        }else {
            return Result.respone(ResultEmnu.ERROR);
        }
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public Result selectAll(){
        try {
                TbUser user =(TbUser)SecurityUtils.getSubject().getPrincipal();
                List<TbAddress> tbAddress = orderService.selectOne(user.getUsername());
                List<TbProvinces> tbProvinces =  orderService.selectTwo();
                List<TbCities> tbCities = orderService.selectThree();
                List<TbAreas> tbAreas = orderService.selectFour();
                Map map = new HashMap();
                map.put("TbAddress",tbAddress);
                map.put("TbProvinces",tbProvinces);
                map.put("TbCities",tbCities);
                map.put("TbAreas",tbAreas);
                return Result.respone(ResultEmnu.SUCCESS).setData(map);
        }catch (Exception e){
                e.printStackTrace();
                return  Result.respone(ResultEmnu.ERROR);
        }

    }
    @RequestMapping("add")
    public String add(Model model){
        List<TbProvinces> plist =  orderService.selectProvinces();
        model.addAttribute("plist",plist);
        return "/add";
    }
    @RequestMapping("getClist")
    @ResponseBody
    public Result getTbCities(String province){
       List<TbCities>  citiesList = orderService.getClist(province);
       try{
           return Result.respone(ResultEmnu.SUCCESS).setData(citiesList);
       }catch (Exception e){
           e.printStackTrace();
           return Result.respone(ResultEmnu.ERROR);
       }
    }
    @RequestMapping("getAlist")
    @ResponseBody
    public Result getAlareasList(String areaid){
        List<TbAreas> areasList = orderService.getAlareasList(areaid);
        try {
            return Result.respone(ResultEmnu.SUCCESS).setData(areasList);
        }catch (Exception e){
            return Result.respone(ResultEmnu.ERROR).setData(areasList);
        }
    }
    @RequestMapping("getCityByPid")
    @ResponseBody
    public Result getCityByPid(String provinceid){
       List<TbCities> tbCitiesList =  orderService.getCityByPid(provinceid);
       return Result.respone(ResultEmnu.SUCCESS).setData(tbCitiesList);
    }
    @RequestMapping("getAreasByPid")
    @ResponseBody
    public Result getAreasCityid(String cityid){
       List<TbAreas>  tbAreasList = orderService.getAreasCityid(cityid);
       return Result.respone(ResultEmnu.SUCCESS).setData(tbAreasList);
    }
    @RequestMapping("addInfo")
    @ResponseBody
    public Boolean addInfo(TbAddress tbAddress){
        TbUser  user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        tbAddress.setUserId(user.getUsername());
        tbAddress.setCreateDate(new Date());
        boolean result =  orderService.insert(tbAddress);
        return result;
    }
    @RequestMapping("deleteByIds/{id}")
    @ResponseBody
    public Integer deleteByIdS(@PathVariable("id")Long id){
        int i = orderService.deleteByIdS(id);
        return i;
    }
    @RequestMapping("findOrderSend")
    @ResponseBody
    public Result findOrderSend(){
        TbUser user =  (TbUser)SecurityUtils.getSubject().getPrincipal();
        List<OrderVO> orderList =  orderService.findOrderSend(user);
        return Result.respone(ResultEmnu.SUCCESS).setData(orderList);
    }


}
